package com.example.myapplication2;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityOptionsCompat;

import java.util.Date;

public class LoginActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    Button passButton;
    CheckBox passwordCheckBox;
    RadioButton LoginTypePass;
    RadioButton LoginTypeCode;
    EditText accountEdit;
    EditText codeEdit;

    ActivityResultLauncher<Intent> intentActivityResultLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //按钮监听
        passButton = findViewById(R.id.passButton);
        passButton.setOnClickListener(this);
        findViewById(R.id.loginButton).setOnClickListener(this);

        //记住密码
        passwordCheckBox = findViewById(R.id.passwordCheckBox);
        passwordCheckBox.setOnClickListener(this);

        //单选类型
        LoginTypePass = findViewById(R.id.LoginTypePass);
        LoginTypeCode = findViewById(R.id.LoginTypeCode);
        //文本框
        accountEdit = findViewById(R.id.accountEdit);
        codeEdit = findViewById(R.id.codeEdit);
        //文本监听
        accountEdit.addTextChangedListener(new TextWatcher3(accountEdit,11));
        codeEdit.addTextChangedListener(new TextWatcher3(codeEdit,6));
        //单选监听
        RadioGroup radioGroup = findViewById(R.id.radioGroupType);
        radioGroup.setOnCheckedChangeListener(this);
        //接受返回参数的
        intentActivityResultLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Intent data = result.getData();
                if (data!=null) {
                    Bundle extras = data.getExtras();
                    if (extras!=null) {
                        if(extras!=null){
                            String message = extras.getString("message");
                            codeEdit.setText(message);
                        }
                    }
                }
            }
        });
    }

    //点击登录类型出现的事件
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.LoginTypePass:
                //密码登录
                passButton.setText("忘记密码");
                passwordCheckBox.setVisibility(View.VISIBLE);
                break;
            case R.id.LoginTypeCode:
                //验证码登录
                passButton.setText("获取验证码");
                //设置选择框不可见
                passwordCheckBox.setVisibility(View.GONE);
                break;
        }
    }
    //点击按键触发的事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.passButton:
                if (LoginTypePass.isChecked()) {
                    String s = accountEdit.getText().toString();
                    if (s!=null){
                        //密码的情况下
                        Intent intent = new Intent(this, ForgetActivity.class);

                        Bundle bundle = new Bundle();
                        bundle.putString("date", s);
                        intent.putExtras(bundle);
                        intentActivityResultLauncher.launch(intent);
                    }else {
                        Toast.makeText(this,"账号异常",Toast.LENGTH_SHORT).show();
                    }


                } else {
                    String s = accountEdit.getText().toString();
                    if (s!=null&&s.length()>5){
                        //验证码的情况下
                        Toast.makeText(this,s+"验证码发送.......",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(this,"账号异常",Toast.LENGTH_SHORT).show();
                    }

                }
                break;
            case R.id.loginButton:
                String k=LoginTypePass.isChecked()?"密码":"验证码";
                String account = accountEdit.getText().toString();
                String code = codeEdit.getText().toString();
                Toast.makeText(this,"登录类型为"+k+"账号为"+account+k+"为"+code,Toast.LENGTH_SHORT).show();
                break;
        }
    }

    //文本监听实现类
    private class TextWatcher3 implements TextWatcher {
        EditText view;
        int i;
        public TextWatcher3(EditText edit, int i) {
            this.view=edit;
            this.i=i;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String s = editable.toString();
            if (s!=null&&s.length()==i){
                //获取输入管理器
                InputMethodManager systemService = (InputMethodManager) LoginActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                //关闭键盘
                systemService.hideSoftInputFromWindow(view.getWindowToken(),0);
            }
        }
    }
}